#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
class Solution1
{
public:
    int monotoneIncreasingDigits(int n)
    {
        string num = to_string(n);
        int pos = 0;
        for (pos = 1;pos < num.size();pos++)
            if (num[pos] < num[pos - 1]) break;
        int i;
        for (i = pos - 1;i >= 0;i--)
            if (i==0 || num[i] != num[i - 1]) break;
        num[i] = num[i] - 1;
        for (++i;i < num.size();i++)
            num[i] = '9';
        return stoi(num);
    }
};
class Solution8 {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals)
    {
        vector<vector<int>> ret;
        sort(ret.begin(), ret.end());
        return ret;
    }
};
//class Solution
//{
//public:
//    int integerReplacement(int n)
//    {
//        vector<int> memo(n);
//        memo[1] = 0;
//        return dfs(n, memo);
//    }
//    int dfs(int n, vector<int>& memo)
//    {
//        if (memo[n] != INT_MAX) return memo[n];
//        int ret;
//        if (n % 2 == 0)
//            ret = dfs(n / 2, memo) + 1;
//        else if (n % 2 == 1)
//            ret = min(dfs(n + 1, memo), dfs(n - 1, memo));
//        memo[n] = min(ret, memo[n]);
//        return ret;
//    }
//};
//class Solution
//{
//public:
//    int integerReplacement(int n)
//    {
//        vector<double> memo(n*2, INT_MAX);
//        memo[1] = 0;
//        return dfs(n, memo);
//    }
//    double dfs(int n, vector<double>& memo)
//    {
//        if (memo[n] != INT_MAX) return memo[n];
//        double ret;
//        if (n % 2 == 0)
//            ret = dfs(n / 2, memo) + 1;
//        else if (n % 2 == 1)
//            ret = min(dfs(n + 1, memo), dfs(n - 1, memo))+1;
//        memo[n] = min(ret, memo[n]);
//        return ret;
//    }
//};
class Solution66
{
public:
    int integerReplacement(int n)
    {
        unordered_map<int, int> memo;
        memo[1] = 0;
        vector<int> v;
        return dfs(n, memo);
    }
    int dfs(int n, unordered_map<int, int>& memo)
    {
        if (memo.count(n)) return memo[n];
        int ret;
        memo[n] = INT_MAX;
        if (n % 2 == 0)
            ret = dfs(n / 2, memo) + 1;
        else if (n % 2 == 1)
            ret = min(dfs(n + 1, memo), dfs(n - 1, memo)) + 1;
        memo[n] = min(ret, memo[n]);
        return ret;
    }
};
class Solution
{
public:
    int maxEnvelopes(vector<vector<int>>& envelopes)
    {
        sort(envelopes.begin(), envelopes.end(), [&](vector<int>& v1, vector<int>& v2) {
            return v1[0] == v2[0] ? v1[1] > v2[1]:v1[0] < v2[0];
            });
        vector<int> ret;
        ret.push_back(envelopes[0][1]);
        for (int i = 1;i < envelopes.size();i++)
        {
            if (envelopes[i][1] > ret.back()[1]) ret.push_back(envelopes[i][1]);
            else
            {
                int left = 0, right = ret.size() - 1;
                while (left < right)
                {
                    int mid = (left + right) / 2;
                    if (ret[mid] >= envelopes[i][1]) right = mid;
                    else left = mid + 1;
                }
                ret[left] = envelopes[i][1];
            }
        }
        return ret.size();
    }
};
int main()
{
    /* Solution().monotoneIncreasingDigits(10);*/
    

	return 0;
}